import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { RuKuService } from './ruku.service';
import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { RukuCreateDialogComponent } from './ruku-create-dialog.component';

@Component({
  selector: 'app-ruku-list',
  templateUrl: './ruku-list.component.html',
  styleUrls: ['./ruku-list.component.scss'],
  standalone: true,
  imports: [CommonModule, FormsModule, ReactiveFormsModule, RukuCreateDialogComponent],
})
export class RuKuListComponent implements OnInit {
  searchForm: FormGroup;
  dataSource: any[] = [];
  total = 0;
  pageIndex = 1;
  pageSize = 10;
  loading = false;
  Math = Math; // 添加Math对象以便在模板中使用
  showCreateDialog = false;

  ruTypeOptions: any[] = [];
  ruStatusOptions: any[] = [];
  storehouseOptions: any[] = [];
  connectNoOptions: any[] = [];

  constructor(
    private fb: FormBuilder,
    private rukuService: RuKuService
  ) {
    this.searchForm = this.fb.group({
      ruKuNo: [''],
      itemName: [''],
      ruKuType: [''],
      ruStatus: [''],
      storehouseName: [''],
      connectNo: [''],
      startDate: [''],
      endDate: ['']
    });
  }

  ngOnInit(): void {
    this.loadEnums();
    this.loadStorehouses();
    this.loadData();
  }

  loadEnums() {
    this.rukuService.getRuTypeEnum().subscribe(res => {
      this.ruTypeOptions = Array.isArray(res) ? res : (res.items || []);
    });
    this.rukuService.getRuStatusEnum().subscribe(res => {
      this.ruStatusOptions = Array.isArray(res) ? res : (res.items || []);
    });
  }

  loadStorehouses() {
    this.rukuService.getStorehouseList({ maxResultCount: 100 }).subscribe(res => {
      this.storehouseOptions = Array.isArray(res) ? res : (res.items || []);
    });
  }

  loadData() {
    this.loading = true;
    const params = {
      Sorting: '',
      SkipCount: (this.pageIndex - 1) * this.pageSize,
      MaxResultCount: this.pageSize
    };
    this.rukuService.getRuKuList(params).subscribe(res => {
      this.dataSource = Array.isArray(res.items) ? res.items : [];
      this.total = res.totalCount || 0;
      this.loading = false;
    }, _ => this.loading = false);
  }

  search() {
    this.loading = true;
    const params: any = {
      RuKuNo: this.searchForm.value.ruKuNo,
      RuKuType: this.searchForm.value.ruKuType,
      RuKuStatus: this.searchForm.value.ruStatus,
      ItemName: this.searchForm.value.itemName,
      Sorting: '',
      SkipCount: (this.pageIndex - 1) * this.pageSize,
      MaxResultCount: this.pageSize
    };
    if (this.searchForm.value.storehouseName) {
      params.StorehouseName = this.searchForm.value.storehouseName;
    }
    this.rukuService.getRuKuListByCondition(params).subscribe(res => {
      this.display(res);
    }, _ => this.loading = false);
  }

  display(res: any) {
    this.dataSource = Array.isArray(res.items) ? res.items : [];
    this.total = res.totalCount || 0;
    this.loading = false;
  }

  onSearch() {
    this.pageIndex = 1;
    this.search();
  }

  onReset() {
    this.searchForm.reset({
      ruKuNo: '',
      ruKuType: '',
      ruStatus: '',
      storehouseName: '',
      connectNo: '',
      startDate: '',
      endDate: ''
    });
    this.pageIndex = 1;
    this.loadData();
  }

  onPageChange(page: number) {
    this.pageIndex = page;
    this.loadData();
  }

  onPageSizeChange(size: number) {
    this.pageSize = size;
    this.pageIndex = 1;
    this.loadData();
  }

  searchStorehouse(keyword: string) {
    this.rukuService.getStorehouseList({ filter: keyword, skipCount: 0, maxResultCount: 20 })
      .subscribe(res => this.storehouseOptions = Array.isArray(res.items) ? res.items : []);
  }

  searchConnectNo(keyword: string) {
    this.rukuService.getConnectNoList({ filter: keyword, skipCount: 0, maxResultCount: 20 })
      .subscribe(res => this.connectNoOptions = Array.isArray(res.items) ? res.items : []);
  }

  getRuTypeText(val: any) {
    const found = this.ruTypeOptions.find(x => x.value === val);
    return found ? found.label : val;
  }

  getRuStatusText(val: any) {
    const found = this.ruStatusOptions.find(x => x.value === val);
    return found ? found.label : val;
  }

  openCreateDialog() {
    this.showCreateDialog = true;
  }
  closeCreateDialog() {
    this.showCreateDialog = false;
  }
  saveCreateDialog(data: any) {
    // 这里可以调用service保存数据
    this.showCreateDialog = false;
    // 可选：保存后刷新列表
    this.loadData();
  }
} 